home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / COMMON_F / GETOPT.C < prev    next >
Text File  |  1990-03-02  |  2KB  |  58 lines

  1. /*
  2.  * getopt.c -- get command-line options.
  3.  */
  4.  
  5. #include "::h:config.h"
  6.  
  7. extern char* progname;
  8.  
  9. /*
  10.  * Based on a public domain implementation of System V
  11.  *  getopt(3) by Keith Bostic (keith@seismo), Aug 24, 1984.
  12.  */
  13.  
  14. #define BadCh    (int)'?'
  15. #define EMSG    ""
  16. #define tell(m)    fprintf(stderr,"%s: %s -- %c\n",progname,m,optopt);return BadCh;
  17.  
  18. int optind = 1;        /* index into parent argv vector */
  19. int optopt;        /* character checked for validity */
  20. char *optarg;        /* argument associated with option */
  21.  
  22. int getopt(nargc,nargv,ostr)
  23. int nargc;
  24. char **nargv, *ostr;
  25.    {
  26.    static char *place = EMSG;        /* option letter processing */
  27.    register char *oli;            /* option letter list index */
  28.    char *index();
  29.  
  30.    if(!*place) {            /* update scanning pointer */
  31.       if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  32.          return(EOF);
  33.       if (*place == '-') {        /* found "--" */
  34.          ++optind;
  35.          return(EOF);
  36.          }
  37.       }                    /* option letter okay? */
  38.    if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  39.       if(!*place) ++optind;
  40.       tell("illegal option");
  41.       }
  42.    if (*++oli != ':') {            /* don't need argument */
  43.       optarg = NULL;
  44.       if (!*place) ++optind;
  45.       }
  46.    else {                /* need an argument */
  47.       if (*place) optarg = place;    /* no white space */
  48.       else if (nargc <= ++optind) {    /* no arg */
  49.          place = EMSG;
  50.          tell("option requires an argument");
  51.          }
  52.       else optarg = nargv[optind];    /* white space */
  53.       place = EMSG;
  54.       ++optind;
  55.       }
  56.    return(optopt);            /* dump back option letter */
  57.    }
  58.